package com.example.factory;

import com.example.inter.YinScan;
import com.example.mapper.PersonMapper;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AbstractClassTestingTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MybatisImportBeanDefinitionRegister
 * @Description
 * @Author Yin
 * @Date 2021/4/8 16:16
 * @Version 1.0
 */

public class MybatisImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        // 扫描
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(YinScan.class.getName());
        String path = (String)annotationAttributes.get("value");
        MybatisScanner mybatisScanner = new MybatisScanner(registry);
        mybatisScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                return true;
            }
        });
        int scan = mybatisScanner.scan(path); //Spring扫描器不关心接口，但这里只关心接口。
    }
}
